


//
//  HMAccount.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/9.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMAccount.h"

@implementation HMAccount

+ (instancetype)accountWithDict:(NSDictionary *)dic{
    HMAccount *account = [[HMAccount alloc] init];
    
    account.access_token = dic[@"access_token"];
    account.expires_in = dic[@"expires_in"];
    account.uid = dic[@"uid"];
    account.name = dic[@"name"];
    
    
    //确定账号过期时间
    //账号过期时间 = 账号产生时间+账号有效期
    NSDate *now = [NSDate date];
    account.expires_time = [now dateByAddingTimeInterval:account.expires_in.doubleValue];
    
    return account;
}

//当从文件中解析出一个对象的时候调用
//在这个方法中写清楚怎么解析文件中的属性
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
        self.expires_in = [aDecoder decodeObjectForKey:@"expires_in"];
        self.uid = [aDecoder decodeObjectForKey:@"uid"];
        self.expires_time = [aDecoder decodeObjectForKey:@"expires_time"];
        self.name = [aDecoder decodeObjectForKey:@"name"];
    }
    return self;
}

//将文件写入对象的时候调用
//在这个方法中写清除,要传参哪些对象的哪些属性,以及怎样存储数据
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.access_token forKey:@"access_token"];
    [aCoder encodeObject:self.expires_in forKey:@"expires_in"];
    [aCoder encodeObject:self.uid forKey:@"uid"];
    [aCoder encodeObject:self.expires_time forKey:@"expires_time"];
    [aCoder encodeObject:self.uid forKey:@"name"];
}

@end
